package com.lcg.exchange.security;

import com.lcg.exchange.model.Role;
import com.lcg.exchange.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    private MySecurityDataService mySecurityDataService;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = mySecurityDataService.findUserByName(s);
        if (user == null)
            return null;
        List<Role> roleListByUserId = mySecurityDataService.findRoleListByUserId(user.getUserId());
        return new MyUserDetails(user,roleListByUserId);
    }
}
